package com.million.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.R;
import com.million.eduservice.entity.EduCourse;
import com.million.eduservice.entity.EduTeacher;
import com.million.eduservice.service.EduCourseService;
import com.million.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: studyboy
 * @Date: 2022/4/6  12:02
 */
@RestController
@RequestMapping("/eduservice/teacherFront")
public class TeacherFrontController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private EduCourseService eduCourseService;

    //前端页面分页查询讲师列表
    @GetMapping("/getTeacherFrontPage/{page}/{limit}")
    public R getTeacherFrontPage (@ApiParam(name = "page", value = "当前页码", required = true)
                                  @PathVariable("page") Long page,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)
                                  @PathVariable("limit") Long limit) {
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        Map<String, Object> map = eduTeacherService.getTeacherFrontPage(teacherPage);
        //如果直接返回了teacherPage，就没有hasnext 和上一页了，这里写底层一些，后台管理
        //的页面用的时elment-ui
        return R.ok().data(map);
    }

    //显示讲师详情功能
    @GetMapping("/getTeacherFrontInfo/{teacherId}")
    public R getTeacherFrontInfo (@PathVariable("teacherId") String teacherId) {

        //1 根据老师id 查询老师基本信息
        EduTeacher teacher = eduTeacherService.getById(teacherId);

        //2. 根据老师id 查询老师授课信息
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacherId);
        List<EduCourse> courseList = eduCourseService.list(queryWrapper);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }
}
